<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class PermissionRequest extends FormRequest
{

    /**
     *
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Determine if the user is authorized to make this request.
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name' => 'required',
            'code'  => 'required|unique:permission',
            'module' => 'required'
        ];
        // 编辑的话，规则有修改
        if ($this->isMethod('put')) {
            $id = $this->route('permission');
            $rules['code'] = "sometimes|required|unique:permission,code,{$id}";
        }
        return $rules;
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '名称必填',
            'code.required' => '标示必填',
            'code.unique' => '标示唯一',
            'module.required' => '模块必填'
        ];
    }
}
